#!/bin/sh
. rt.comm
cur_pwd=`pwd`
if [ "$1" == "-j" ];then
    target_dir="~"
    cd ~
    cur_path=`pwd -P`
    config_file=$cur_path/.rtrc
    if [ ! -f "$config_file" ];then
        echo your jump file not exist, please add one ~/.rtrc 
        exit 0
    fi

else
    #echo rt_init
    rt_init
    #echo target_dir=`dirname "$1"`
    target_dir=`dirname "$1"`
    #target_base=`basename "$1"`
    #if [ -d "$target_dir/$target_base" ];then
    #    target_dir=$target_dir/$target_base
    #fi
    #echo cd "$target_dir"
    cd "$target_dir"
    
    cur_path=`pwd -P`
    config_file=$cur_path/.rt.config
fi

is_exist_rt_configfile=no
parent_dir=`dirname $cur_path`
tmp_base=`basename $cur_path`
tmp_dir_rest=""
#echo loop start
loop_flag=1
while [ "$loop_flag" == "1" ];do
    if [ -f "$config_file" ]; then 
        is_exist_rt_configfile=yes
        loop_flag=0
    else
        cur_path=$parent_dir
        #echo "cur_path=$cur_path"
        tmp_dir_rest="/$tmp_base$tmp_dir_rest"


        #echo "tmp_dir_rest=$tmp_dir_rest"
        config_file=$cur_path/.rt.config
        #echo "config_file=$config_file"
        tmp_base=`basename $cur_path`
        parent_dir=`dirname $cur_path`
        if [ "$parent_dir" == "/" ]; then
            is_exist_rt_configfile=no
            loop_flag=0
        fi
    fi
done
#echo loop end

if [ "$is_exist_rt_configfile" = "no" ]; then 
    cd "$cur_pwd"
    exit 0  
fi
#cat $config_file| sed "s/^/export g_/g" |sh

dst_ip="INIT_NULL"
dst_port="INIT_NULL"
dst_dir="INIT_NULL"
dst_user="INIT_NULL"
dst_pwd="INIT_NULL"
jump_ip="INIT_NULL"
jump_port="INIT_NULL"
jump_tmpdir="INIT_NULL"
jump_user="INIT_NULL"
jump_pwd="INIT_NULL"
while read line
do
    #echo "$line"
    #echo  ${line%%=*}=${line#*=}
    eval  ${line%%=*}=${line#*=}
done<  $config_file

if [ "INIT_NULL" = "$dst_ip" ];then
    dst_ip=$rt_dst_ip
fi
if [ "INIT_NULL" = "$dst_port" ];then
    dst_port=$rt_dst_port
fi
if [ "INIT_NULL" = "$dst_dir" ];then
    dst_dir=$rt_dst_dir
fi
if [ "INIT_NULL" = "$dst_user" ];then
    dst_user=$rt_dst_user
fi
if [ "INIT_NULL" = "$dst_pwd" ];then
    dst_pwd=$rt_dst_pwd
fi
if [ "INIT_NULL" = "$jump_ip" ];then
    jump_ip=$rt_jump_ip
fi
if [ "INIT_NULL" = "$jump_port" ];then
    jump_port=$rt_jump_port
fi
if [ "INIT_NULL" = "$jump_tmpdir" ];then
    jump_tmpdir=$rt_jump_tmpdir
fi
if [ "INIT_NULL" = "$jump_user" ];then
    jump_user=$rt_jump_user
fi
if [ "INIT_NULL" = "$jump_pwd" ];then
    jump_pwd=$rt_jump_pwd
fi

if [ -z "$dst_ip" ];then
    dst_ip="null"
fi
if [ -z "$dst_port" ];then
    dst_port=22
fi
if [ -z "$dst_dir" ];then
    dst_dir="null"
fi
if [ -z "$dst_user" ];then
    dst_user="null"
fi
if [ -z "$dst_pwd" ];then
    dst_pwd="null"
fi
if [ -z "$jump_ip" ];then
    jump_ip="null"
fi
if [ -z "$jump_port" ];then
    jump_port=22
fi
if [ -z "$jump_tmpdir" ];then
    jump_tmpdir="null"
fi
if [ -z "$jump_user" ];then
    jump_user="null"
fi
if [ -z "$jump_pwd" ];then
    jump_pwd="null"
fi

echo "dst_ip=$dst_ip"
echo "dst_port=$dst_port"
echo "dst_dir=$dst_dir$tmp_dir_rest"
echo "dst_user=$dst_user"
echo "dst_pwd=$dst_pwd"
echo "jump_ip=$jump_ip"
echo "jump_port=$jump_port"
echo "jump_tmpdir=$jump_tmpdir"
echo "jump_user=$jump_user"
echo "jump_pwd=$jump_pwd"

cd "$cur_pwd"
exit 0
